package cn.rokhdelar.umbrsapi.oss.controller;

import cn.rokhdelar.umbrsapi.oss.entity.WangEditImgInfo;
import cn.rokhdelar.umbrsapi.oss.service.OSSService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/oss")
public class OSSController {
    private final OSSService ossService;

    public OSSController(OSSService ossService) {
        this.ossService = ossService;
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam MultipartFile file) {
        return ResponseEntity.ok(ossService.putObject(file, ""));
    }

    @PostMapping("/upload1")
    public ResponseEntity<JSONObject> uploadFile1(@RequestParam MultipartFile file) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("errno",0);
        List<WangEditImgInfo> list=new ArrayList<>();
        WangEditImgInfo imgInfo=new WangEditImgInfo();
        imgInfo.setUrl(ossService.putObject(file, ""));
        list.add(imgInfo);
        jsonObject.put("data",list);
        return ResponseEntity.ok(jsonObject);
    }

    @PostMapping("/upload2")
    public ResponseEntity<WangEditImgInfo> uploadFile2(@RequestParam MultipartFile file) {
        WangEditImgInfo jsonObject=new WangEditImgInfo();
        jsonObject.setUrl(ossService.putObject(file, ""));
        return ResponseEntity.ok(jsonObject);
    }
}
